package cn.itcast.service.impl;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.entity.OrderSettingVo;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired//预约设置数据相关
    private OrderSettingDao orderSettingDao;

    /**
     * 查询当月预约的人数以及可预约的日期
     *
     * @param
     * @return
     */
    @Override
    public List<OrderSettingVo> orderSettingFindAll(Integer Year, Integer Month) {
        //查询到指定月份的所有预约数据
        List<OrderSetting> orderSettings = this.orderSettingDao.orderSettingFindAll(Year, Month);
        //将年月日转换为日期
        List<OrderSettingVo> orderSettingVos = new ArrayList<>();
        DateFormat format = new SimpleDateFormat("dd");
        //数据填充
        for (OrderSetting orderSetting : orderSettings) {
            Integer date = Integer.valueOf(format.format(orderSetting.getOrderDate()));
            OrderSettingVo orderSettingVo = new OrderSettingVo();
            orderSettingVo.setDate(date);//当月日期
            orderSettingVo.setNumber(orderSetting.getNumber());//预约数
            orderSettingVo.setReservations(orderSetting.getReservations());//已预约数
            orderSettingVos.add(orderSettingVo);

        }
        return orderSettingVos;
    }

    /**
     * 保存用户输入的预约数据
     *
     * @param param
     * @return
     */
    @Transactional
    @Override
    public void saveOrderSetting(Map<String, String> param) {
        //先查询是否存在过此预约数据
        Integer id = this.orderSettingDao.orderSettingFindById(param.get("orderDate"));
        if (id != null) {
            Integer number = Integer.valueOf(param.get("number"));
            this.updateOrderSetting(id, number);
            return;
        }
        OrderSetting orderSetting = new OrderSetting();
        try {
            orderSetting.setOrderDate(DateUtils.parseString2Date(param.get("orderDate")));
            orderSetting.setNumber(Integer.parseInt(param.get("number")));
            this.orderSettingDao.saveOrderSetting(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量保存
     *
     * @param orderSettings
     */
    @Override
    @Transactional
    public void add(List<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            //先查询是否存在过此预约数据
            Integer id = null;
            try {
                id = this.orderSettingDao.orderSettingFindById(DateUtils.parseDate2String(orderSetting.getOrderDate()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (id != null) {
                this.updateOrderSetting(id, orderSetting.getNumber());
            } else {
                this.orderSettingDao.saveOrderSetting(orderSetting);
            }
        }
    }

    /**
     * 修改预约数据
     *
     * @param id
     * @param number
     */
    @Transactional
    public void updateOrderSetting(Integer id, Integer number) {
        try {
            //根据id进行修改预约数
            this.orderSettingDao.updateOrderSetting(id, number);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<Map<String, Object>> findById() {

        return orderSettingDao.findById();
    }

    @Override
    public List<Integer> findSettingByMonths(List<String> months) {
        //容器
        List<Integer> data = new ArrayList<>();
        for (String month : months) {
            String max = month + "-31";
            String min = month + "-1";

            int count = this.orderSettingDao.findSettingByMonths(max, min);

            data.add(count);

        }

        return data;
    }


}
